<?php
$mypage = "rex_lang";

$Basedir = dirname(__FILE__);
$entry_id = !empty($entry_id) ? (int) $entry_id : 0;
$mode = !empty($mode) ? (string) $mode: ''; 
$func = !empty($func) ? (string) $func: '';

if((isset($_POST['rexform_delete_button']))&&($_POST['s1_id']!="")) 
{
	if ($_POST['s1_id'] > 0)
	{
		rex_deleteCLang($_POST['s1_id']);
		$message = $I18N->msg('clang_deleted');
	}
}

if (!empty($_POST['s1_id'])&&($func=='add'))
{
	if ($_POST['s1_name'] != '' && isset($_POST['s1_id']))
	{
		if (!array_key_exists($_POST['s1_id'], $REX['CLANG']))
		{
			$message = $I18N->msg('clang_created');
			rex_addCLang($_POST['s1_id'], $_POST['s1_name']);
		} else {
			$message = $I18N->msg('id_exists');
		}
	} else {
		$message = $I18N->msg('enter_name');
	}
}
elseif (isset($_POST['entry_id'])&&($func=='edit'))
{
	if (isset($_POST['entry_id']))
	{
		rex_editCLang($_POST['s1_id'], $_POST['s1_name']);
		$message = $I18N->msg('clang_edited');
	}
}
unset($_POST['s1_name']);
unset($s1_name);


if (isset($_POST['s1_id'])&&isset($_POST['s1_priority'])&&($func=='edit'))
{
  	// create new whatever
	$entry_id = $_POST['s1_id'];
	$sql = new sql();
	$sql->setQuery("select id, priority from rex_393_language");
		$geberArr=array();
	for ($i=0;$i<$sql->getRows();$i++) {
		if ($sql->getValue('id')==$_POST['s1_id'])
			$priority = ($_POST['s1_priority']*100-1);
		else {
			$priority = ($sql->getValue('priority')*100+$i);
		}
		$geberArr[$priority] = $sql->getValue('id');
		$sql->next();		
	}
	
	unset($_POST['s1_priority']);
	ksort($geberArr);
	reset($geberArr);
	$sql = new rex_sql();
//	$sql->debug=true;	
	$sql->setTable('rex_393_language');
	for ($i=0;$i<sizeof($geberArr);$i++) {
		$sql->setWhere('id='.current($geberArr));
		$sql->setValue('priority',$i);
		$sql->update("success");
		next($geberArr);
	}
}
//------------------------------> Eintragsliste
if ($func == '')
{
	require_once $Basedir.'/../../addon_framework/classes/list/class.rex_list.inc.php';

/**
*  create list
*/

	$sql = 'select * from rex_393_language';
  

	$list = new rexlist($sql, 'priority', 'asc');
	$list->setLabel($I18N_393->msg('label_language_list'));
//	$list->debug = true;

	$col = new resultColumn('id', $I18N_393->msg('label_id'));
	$col->setParams(array ('func' => 'edit', 'entry_id' => '%id%'));
	$cols[] = $col;
	
	$col = new resultColumn('id', $I18N->msg('clang_name'));

	foreach ($REX['CLANG'] as $lang_id => $lang)
		$col->addCondition('id', $lang_id, htmlspecialchars($lang)); 
	$cols[] = $col;

	if ($xtc) {
		$col = new resultColumn('xtcommerce_id', $I18N_393->msg('label_xtcommerce_id'));
		$col->delOption(OPT_SORT, OPT_FILTER);
		$cols[] = $col;
	}

	$col = new resultColumn('meta_language', $I18N_393->msg('label_meta_language'));
	$col->delOption(OPT_SORT, OPT_FILTER);
	$cols[] = $col;
	$col = new resultColumn('priority', $I18N_393->msg('label_priority'));
    $cols[] = $col;

	for ($i=0;$i<sizeof($cols);$i++)
		$list->addColumn($cols[$i]); 
		
	$list->setVar(LIST_VAR_BEFORE_DATA, '<tr><td colspan="5"><a href="./index.php?page=' . $mypage . '&subpage=lang&stepping=16&func=add">' . $I18N_393->msg('add_language') . '</a></td></tr>');



	$list->setStepping(16);
	$list->show(false);

} elseif ($func == 'edit' || $func == 'add') {
  
	require_once $Basedir.'/../../addon_framework/classes/form/class.rex_form.inc.php';

	$form = & new rexForm('structure_form');
//	$form->debug = true;
	$form->setApplyUrl('index.php?page='.$mypage.'&subpage=lang');
  
	$form->setEditMode(isset($_GET['entry_id']) || isset($_POST['entry_id']));
	
	//------------------------------> Hidden Fields
	$fieldFunc = & new hiddenField('func');
	if (isset($_GET['entry_id']) || isset($_POST['entry_id'])) $fieldFunc->setValue('edit'); else  $fieldFunc->setValue('add');

	if (isset($_GET['entry_id']) || isset($_POST['entry_id'])) {
		$fieldEntryId = & new hiddenField('entry_id');
		$fieldEntryId->setValue("$entry_id");
	}
  
	//------------------------------> Fields[general]
	$fields = array();
	if($func == 'add')
	{
		$field = & new selectField('id', $I18N_393->msg('label_id'));

		foreach (array_diff(range(0, 14), array_keys($REX['CLANG'])) as $clang)
			$field->addOption($clang, $clang);
		$field->addAttribute('size', '1');
	} else {
		$field = & new readOnlyTextField('id', $I18N_393->msg('label_id'));
	}
	$fields[]=$field;
  
  
	$field = & new textField('name', $I18N_393->msg('label_language'));
	$field->activateSave(false);
	if (isset($_GET['entry_id']) || isset($_POST['entry_id'])) $field->setValue($REX['CLANG'][$entry_id]);
	$fields[]=$field;
  
  	if ($xtc) {
		$field = & new selectField('xtcommerce_id', $I18N_393->msg('label_xtcommerce_id'));
		$field->addOption($I18N_393->msg('opt_no_xtc_conn'),'');
		$field->addSQLOptions('SELECT name,languages_id FROM languages');
		$field->addAttribute('size', '1');
		$fields[]=$field;
	}
    
	$field = & new rexMediaButtonField('lang_icon', $I18N_393->msg('label_lang_icon'));
	$fields[]=$field;
	$field = & new rexMediaButtonField('lang_icon_over', $I18N_393->msg('label_lang_icon_over'));
	$fields[]=$field;

	$field = & new textField('meta_language', $I18N_393->msg('label_meta_language'));
	$fields[]=$field;
  
	$field = & new textField('priority', $I18N_393->msg('label_priority'));
	$fields[]=$field;
	
	$field = & new selectField('status', $I18N_393->msg('label_status'));
	$field->addOption($I18N_393->msg('status_offline'),'0');
	$field->addOption($I18N_393->msg('status_online'),'1');
	$field->addAttribute('size','1');
	$fields[] = $field;
	
	$whereParam = ($_GET['entry_id']=='' && $_POST['entry_id']=='') ? array('id' => 9999) : array ('id' => $_REQUEST['entry_id']);
	$sectionCommon = & new rexFormSection('rex_393_language', $I18N_393->msg('label_language_form'), $whereParam);

  
	for($i = 0; $i < count($fields); $i++)
		$sectionCommon->addField($fields[$i]);

	$form->addSection($sectionCommon);
	$form->addField($fieldFunc);
	if (isset($fieldEntryId))
		$form->addField($fieldEntryId);
  
	$form->show(true);  
}
?>
<?php
$msg = array();
$error = false;
if (isset($_GET['lg_src']) && isset($_GET['lg_trgt'])) {
	if ($_GET['lg_src'] == $_GET['lg_trgt']) {
		$msg[] = 'Source and target language have to be different.';
		$error = true;
	}
	if (!$error) {
		include($Basedir.'/../functions/copy.inc.php');
		a393_copyLanguage($_GET['lg_src'],$_GET['lg_trgt']);
	
	}
	unset($_GET['lg_src'], $_GET['lg_trgt']);
}
$langOptions = '';
$qry = "SELECT name,id FROM rex_clang";
$sql = new rex_sql;
$langs = $sql->getArray($qry);
foreach ($langs as $myLang)
	$langOptions .= '<option value="'.$myLang['id'].'">'.$myLang['name'].'</option>'."\n";
?>
<?php if ($error != '') { ?>
	<p><?php echo implode('<br />',$msg); ?></p>
<?php } ?>
<div class="rex-cnt-cols">
  <p class="rex-hdl">Artikelinhalte zwischen Sprachen kopieren</p>
  <form method="get" enctype="text/plain" action="./index.php">
  <div class="hidden"><input type="hidden" name="page" value="<?php echo $mypage; ?>" /><input type="hidden" name="subpage" value="lang" /></div>
  <div class="rex-spc-stn-cnt">
    <p><label for="lg_src">Ursprung Sprache:</label><select id="lg_src" name="lg_src"><?php echo $langOptions; ?></select></p>
	<p><label for="lg_trgt">Ziel Sprache:</label><select id="lg_trgt" name="lg_trgt"><?php echo $langOptions; ?></select></p>
	<p>
            <input type="submit" title="Kopieren" value="Kopieren" name="sendit" class="rex-sbmt"/>
          </p>
  </div>
  </form>
</div>